package com.sistemagestion.clientes.view;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.EditText;
import android.widget.ListView;

import com.sistemagestion.R;
import com.sistemagestion.BO.ClienteBO;
import com.sistemagestion.Entities.Cliente;
import com.sistemagestion.Utils.Constantes;
import com.sistemagestion.Utils.DialogConfirmacion;
import com.sistemagestion.Utils.DialogConfirmacion.IDialogListenerButton;
import com.sistemagestion.Utils.Preferencia;
import com.sistemagestion.clientes.view.DialogOrdenarCliente.ItemListener;
import com.sistemagestion.ventas.view.FacturaEdit;

public class ClienteListado extends FragmentActivity implements IDialogListenerButton, ItemListener {

	private static final int ACTIVITY_ALTA_CLIENTE = 0;
	private static final int ACTIVITY_MODIFICAR_CLIENTE = 1;
	private static final int ACTIVITY_EDITAR_FACTURA = 2;
	private ClienteBO clientebo = new ClienteBO();
	List<Cliente> _clientes;
	ClienteAdapter adapter;
	Cliente _cliente;
	int selectedPos;
	private Preferencia mpreferencia;

	ListView lstClientes;
	EditText txtBuscar;
//	TextView nose;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lyt_listado_clientes);
		mpreferencia = new Preferencia(this);
		InitVars();
		registerForContextMenu(lstClientes);
		
		txtBuscar = (EditText) findViewById(R.id.txtBuscar);
		txtBuscar.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence text, int start, int before,
					int count) {
				
				adapter.getFilter().filter(text.toString());
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mn_lstcliente, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.itmNuevo:
			Intent intent = new Intent(this, ClienteEditActivity.class);
			intent.putExtra("OPERATION", Constantes.Operations.ADD);
			startActivityForResult(intent, ACTIVITY_ALTA_CLIENTE);
			return true;
		case R.id.itmOrdenar:
			DialogOrdenarCliente dialogOrdenarCliente = new DialogOrdenarCliente();
			dialogOrdenarCliente.show(getSupportFragmentManager(), "Ordenar");
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	// Crea el menu contextual
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
		menu.setHeaderTitle("opciones");
		inflater.inflate(R.menu.ctx_mn_lstcliente, menu);
	}

	// Se dispara cuando se presiona un item del menu contextual
	@Override
	public boolean onContextItemSelected(MenuItem item) {

		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		selectedPos = info.position;
		_cliente = adapter.getItem(selectedPos);
		DialogConfirmacion dialogo = new DialogConfirmacion();
		switch (item.getItemId()) {
		case R.id.itmEditar:
			Intent intent = new Intent(this, ClienteEditActivity.class);
			intent.putExtra("OPERATION", Constantes.Operations.EDIT);
			intent.putExtra("CLIENTE", _cliente);
			startActivityForResult(intent, ACTIVITY_ALTA_CLIENTE);
			return true;
		case R.id.itmEliminar:
			dialogo.show(getSupportFragmentManager(), "Eliminar");
			return true;
		case R.id.itmVender:
			showFacturaEdit();
			return true;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void InitVars() {
		_clientes = clientebo.getAll();
		adapter = new ClienteAdapter(this, R.layout.lyt_listado_clientes_item,
				_clientes);
		lstClientes = (ListView) findViewById(R.id.lstClientes);

		lstClientes.setAdapter(adapter);
	}

	public void Eliminar(){
		if (_cliente != null) {
			clientebo.Eliminar(_cliente);
			adapter.remove(_cliente);
		}
	}
	@Override
	public void onPositiveButton() {
		Eliminar();
	}

	@Override
	public void onCancelButton() {

	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == ACTIVITY_ALTA_CLIENTE && resultCode == this.RESULT_OK){
			adapter.remove(_cliente);
			_cliente = clientebo.getById(_cliente.getIdCliente());
			adapter.insert(_cliente, selectedPos);
		}else if(requestCode == ACTIVITY_MODIFICAR_CLIENTE && resultCode == this.RESULT_OK){
			//actualizar
		}
		
	}
	private void showFacturaEdit()
	{
		Intent intent = new Intent(this, FacturaEdit.class);
		intent.putExtra("OPERATION", Constantes.Operations.ADD);
		intent.putExtra("CLIENTE", _cliente);
		startActivityForResult(intent, ACTIVITY_EDITAR_FACTURA);
	}

	@Override
	public void onItemSelected(int position) {
		mpreferencia.setCriterioOrdenCliente(position);
		adapter.sort(position);
	}

}
